package ar.com.spsolutions.splibrary.services;

import java.io.Serializable;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.annotation.Secured;
import org.springframework.transaction.annotation.Transactional;

import ar.com.spsolutions.splibrary.entity.Entity;
import ar.com.spsolutions.splibrary.general.RoleConverterService;
import ar.com.spsolutions.splibrary.notifiers.INotifier;
import ar.com.spsolutions.splibrary.repositories.GenericRepository;

public class TemplateEntityService<E extends Entity> implements Serializable {

	private static final long serialVersionUID = 8254683113939224067L;

	private GenericRepository<E> repository;

	@Autowired
	private INotifier notifier;

	@Transactional(readOnly = true)
	public List<E> findAll() {
		return this.getRepository().findAll();
	}

	@Transactional
	public void add(final E aEntity) {
		this.getRepository().save(aEntity);
	}

	@Transactional
	@Secured(value = { RoleConverterService.ROLE_LIBRARIAN })
	public void deleted(final E aEntity) {
		this.getRepository().delete(aEntity);
	}

	@Transactional
	@Secured(value = { RoleConverterService.ROLE_LIBRARIAN })
	public Entity update(final E aEntity) {
		return this.getRepository().update(aEntity);
	}

	@Transactional
	public E attachSession(final E entity) {
		return this.getRepository().attachSession(entity);
	}

	@Transactional
	public E get(final E entity) {
		return this.getRepository().get(entity);
	}

	protected GenericRepository<E> getRepository() {
		return this.repository;
	}

	public void setRepository(final GenericRepository<E> repository) {
		this.repository = repository;
	}

	public INotifier getNotifier() {
		return this.notifier;
	}

	public void setNotifier(final INotifier notifier) {
		this.notifier = notifier;
	}

}
